{extend name="public/layout" /}
{block name="style"}
    <style>
        .zhong{
            text-align: center;
            margin-top: 5px;
        }
        .widt{
            width: 100%;
            height: 38px;
            line-height: 38px;
            min-width: 50px;
            border-color: #E6E6E6;
        }
    </style>
{/block}
{block name="main"}
<div id="fileList" style="display: none"></div>
<form class="layui-form" style="width: 80%;margin-left: 10%;margin-top: 50px" action="{$action}" method="post" enctype="multipart/form-data">
    {foreach $form as $value}
    {if $value['type']=='none'}
    {include file='form/none'}
    {elseif $value['type']=='input'}
    {include file='form/input'}
    {elseif $value['type']=='text'}
    {include file='form/text'}
    {elseif $value['type']=='pwd'}
    {include file='form/pwd'}
    {elseif $value['type']=='textarea'}
    {include file='form/textarea'}
    {elseif $value['type']=='radio'}
    {include file='form/radio'}
    {elseif $value['type']=='checkbox'}
    {include file='form/checkbox'}
    {elseif $value['type']=='switch'}
    {include file='form/switch'}
    {elseif $value['type']=='file'}
    {include file='form/file'}
    {elseif $value['type']=='files'}
    {include file='form/files'}
    {elseif $value['type']=='img'}
    {include file='form/img'}
    {elseif $value['type']=='imgs'}
    {include file='form/imgs'}
    {elseif $value['type']=='select'}
    {include file='form/select'}
    {elseif $value['type']=='editor'}
    {include file='form/editor'}
    {elseif $value['type']=='time'}
    {include file='form/time'}
    {elseif $value['type']=='dropdown'}
    {include file='form/dropdown'}
    {/if}
    {/foreach}
    {include file='form/formbutton' /}
</form>
{/block}
{block name="javascript"}
<script type="text/javascript">
    {foreach $form as $key=>$value}
    {if $value['type']=='editor'}
    var ue{$key} = UE.getEditor("{$value['name']}");
    {/if}
    {/foreach}
</script>
<script>
    //分片上传
    var penpian = $(".fenpian");
    var penpians = $(".fenpians");
    var fenpian ='';
    var inde = '';
    var chajian = new ChaJian();
    //单文件分片上传
    $.each(penpian,function (i,v) {
        var id = $(v).attr('id');
        console.log(id);
        fenpian = FenPianUpFile("#"+id, '{:url("api/fenpianupfile")}',true,true,2*1024*1024,function (respos,res) {
            layer.close(inde);
            console.log(respos);
            $(v).prev('input').eq(0).val(respos.id);
            layer.msg('文件上传成功');
        },function () {
            inde = layer.load(0, {shade: false});
        },function () {
            console.log('上传下一片')
        },'{:config("webroot")}/static/home/js/Uploader.swf')
    })
    //多文件分片上传
    $.each(penpians,function (i,vs) {
        var id = $(vs).attr('id');
        fenpian = FenPianUpFile("#"+id, '{:url("api/fenpianupfile")}',true,true,1*1024*1024,function (respos,res) {
            var list = $("#"+id).prev('input').eq(0).val();
            if(list&&list!==""){
                list = list.split(',');
                list.push(respos.id);
                list.join(',');
            }else{
                list = respos.id;
            }
//            $(v).prev('input').val(respos.id);
            $("#"+id).prev('input').eq(0).val(list);

            //layer.msg('文件上传成功');
        },function () {
            inde = layer.load(0, {shade: false});
            $("#"+id).prev('input').eq(0).val("");
//            console.log('添加文件后')
        },function () {
            layer.close(inde);
            layer.msg('文件上传完成');
//            console.log('文件上传文昌')
        },'{:config("webroot")}/static/home/js/Uploader.swf')
    });

    var chajian = new ChaJian();
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
                ,layer = layui.layer
                ,layedit = layui.layedit
                ,laydate = layui.laydate;
    var datas = $(".datas");
    $.each(datas,function (i,v) {
        var id = $(v).text();
        laydate.render({
            elem: '#'+id,
            type:'datetime'
        });
    });
    var editors = $(".editors");
    var name = '';
    $.each(editors,function (i,v) {
        var id = $(v).text();
        name = 'edit'+i;
        name = layedit.build(id,{
            uploadImage:{
                url:"{:url('api/addimgs')}",
                post:'post'
            }
        });
        })
    });
    //返回按钮函数
    function back() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

        parent.layer.close(index); //再执行关闭
    }

    //联动下拉框函数
    function lianbiao(tables,action,obj) {
        var pid = $(obj).val();
        console.log(pid);
        if(!pid){
            return;
        }
        $.post(action,{pid:pid,table:tables},function (res) {
            console.log(res);
            var select = $(obj).parent().next('.layui-inline').children('select');
            var str ='<option value="">请选择</option>';
            $.each(res,function (i,v) {
                str+='<option value="'+v.id+'">'+v.name+'</option>';
            });
            select.empty();
            select.append(str);
        })
    }


    //预览图片单图片
    function yulan(obj) {
//        chajian.getFileUrl($(obj),function (src) {
//            $(obj).parents('.layui-form-item').next().children('.show').empty();
//            var str = '<img src="'+src+'" width="150"/>';
//            $(obj).parents('.layui-form-item').next().children('.show').append(str);
//        })
        var data = chajian.upFile($(obj),"{:url('api/addfiles')}");
        console.log(data);
        $(obj).prev().val(data[0].id);
        $(obj).parent().next('.show').empty();
        var str = '<img src="'+data[0].path+'" style="max-height: 100px;max-width: 200px"/>';
        $(obj).parent().next('.show').append(str);
    }
    //预览多图片
    function yulanduotupian(obj) {
        var data = chajian.upFile($(obj),"{:url('api/addfiles')}");
        $.each(data,function (i,v) {
            var str = '<div class="dels" style="float: left;margin-right: 5px">'+
                '<img src="'+v.path+'" style="max-width: 200px;max-height: 100px"/>'+
                '<div class="zhong">'+
                '<input style="" onclick="shanchuimg('+v.id+',this)" type="button" class="layui-btn layui-btn-sm layui-btn-danger" value="删除">'+
                '</div>'+
                '</div>';
            $(obj).parents().next('.shows').append(str);
            var num = $(obj).prev().val();
            num = num.split(',');
            num.push(v.id);
            $(obj).prev().val(num.join(','));
        })
    }
    //删除图片
    function shanchuimg(num,obj) {
        console.log($(obj).parents('.shows').eq(0).prev('.Mfile').eq(0).children('input').eq(0));
//        return;
        $.post("{:url('api/deleteFile')}",{id:num},function (res) {
            if(res.code==200){
                layer.msg(res.msg);
                var str = $(obj).parents('.shows').eq(0).prev('.Mfile').eq(0).children('input').eq(0).val();
                console.log(str);
                str = str.split(',');
                var nums = [];
                $.each(str,function (i,v) {
                    console.log(num);
                    if(parseInt(v)!==num){
                        nums.push(v);
                    }
                })
                console.log(nums);
                $(obj).parents('.shows').eq(0).prev('.Mfile').eq(0).children('input').eq(0).val(nums.join(','))
                $(obj).parents('.dels').eq(0).remove();
            }else{
                layer.msg(res.msg);
            }
        })
    }
    function addfile(obj) {
        chajian.QiNiuUpFile(obj,"{:url('api/getqiniutoken')}",function (newsname,ss,abb) {
            $.post("{:url('api/addqiniu')}",{newname:newsname,yuanname:abb},function (res) {
                $(obj).prev('input').val(res.id);
                layer.msg('上传成功');
            })
        })
    }
    function addfiles(obj) {
        chajian.QiNiuUpFile(obj,"{:url('api/getqiniutoken')}",function (newsname,ss,abb) {
            $.post("{:url('api/addqiniu')}",{newname:newsname,yuanname:abb},function (res) {
                var str = $(obj).prev('input').val();
                str = str.split(',');
                str.push(res.id);
                str = str.join(',');
                $(obj).prev('input').val(str);
            })
            if(ss){
            layer.msg('上传成功');
            }
        })
    }

</script>
{/block}